package com.mpyy.handler.inventory;

import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.DepotInfo;
import com.mpyy.pojo.InventoryCheck;
import com.mpyy.pojo.InventoryCheckDesc;
import com.mpyy.pojo.InventoryDesc;
import com.mpyy.service.inventory.InventoryCheckServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description InventoryCheckController
 * @Author wx
 * @Date 2022-03-07 11:04
 * @Version 1.0
 */
@Controller
public class InventoryCheckController {
    @Autowired
    @Qualifier(value = ("inventoryCheckServiceImpl"))
    private InventoryCheckServiceInter checkServiceInter;

    String inveDepot=null;
    @RequestMapping("/selectCheckProcess")
    @ResponseBody
    public String selectCheckProcess(HttpServletRequest req){
        String ck = req.getParameter("ck");
        inveDepot=ck;
        List <InventoryDesc> inventoryDescs = checkServiceInter.searchCheck(ck);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryDescs.size());
        checkHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/selectCheckIdProcess")
    @ResponseBody
    public String selectCheckIdProcess(HttpServletRequest req){
        String id=req.getParameter("Id");
        List <InventoryDesc> inventoryDescs = checkServiceInter.searchCheckId(id);
        HashMap <Object, Object> objectObjectHashMap = new HashMap <>();
        return JSON.toJSONString(inventoryDescs);
    }
    @RequestMapping("/selectTakeStock")
    @ResponseBody
    public String selectTakeStock(){
        List <InventoryCheck> inventoryDescs = checkServiceInter.searchTakeStock();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryDescs.size());
        checkHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/selectInventoryCheckDesc")
    @ResponseBody
    public String selectInventoryCheckDesc(HttpServletRequest req){
        String checkid=req.getParameter("checkid");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(checkid);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryCheckDescs.size());
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
//    查询盘点单
    @RequestMapping("/selectselectInventorySurplus")
    @ResponseBody
    public String selectselectInventorySurplus(){
        List <InventoryCheck> inventoryChecks = checkServiceInter.searchInventorySurpluslosses();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryChecks.size());
        checkHashMap.put("data",inventoryChecks);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/deleteInventoryCheckList")
    @ResponseBody
    public void deleteInventoryCheckList(HttpServletRequest req){
        String checkid=req.getParameter("checkid");
        int i = checkServiceInter.updateInventory(checkid);
        int i1 = checkServiceInter.updateInventoryList(checkid);
    }
    //删除商品明细单
    @RequestMapping("/deleteInventoryCommodity")
    @ResponseBody
    public void deleteInventoryCommodity(HttpServletRequest req){
        String invCheckDescId=req.getParameter("invCheckDescId");
        String checkid=req.getParameter("checkid");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(checkid);
        int a = inventoryCheckDescs.size();
        int i = checkServiceInter.updateCommodity(invCheckDescId);
        if(a==1){
            int il = checkServiceInter.updateInventoryList(checkid);
        }
    }
    List<String> arrty = new ArrayList <>();
    //选择完盘点单后的所有的商品
    @RequestMapping(value = "/selectSecondStep" , produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectSecondStep(HttpServletRequest req) throws Exception {
        String array = req.getParameter("invId");
        String[] arr=array.split(",");
        List<String> stringList = new ArrayList <>();
        for (String s : arr) {
            stringList.add(s);
        }
        arrty=stringList;
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchInventoryDesc(stringList);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",2);
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
    List<InventoryCheckDesc> checkDescList=null;
    @RequestMapping(value = "/selectTreeStep", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectTreeStep(){
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchInventoryDesc(arrty);
        checkDescList=inventoryCheckDescs;
        for(InventoryCheckDesc o : inventoryCheckDescs){
            int i = o.getInvCheckDescCheckNum() - o.getInvCheckDescInventoryNum();
            o.setQuantityVariance(i);
        }
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",2);
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }

    /**
     * 根据商品编号查询库存
     * @param req
     * @return
     */
    @RequestMapping("/searchDescId")
    @ResponseBody
    public String searchDescId(HttpServletRequest req){
        String proId=req.getParameter("proId");
        InventoryDesc inventoryDesc = new InventoryDesc();
        inventoryDesc.setProId(proId);
        inventoryDesc.setInveDescDepot(inveDepot);
        List <InventoryDesc> inventoryDescs = checkServiceInter.searchDescProId(inventoryDesc);
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(checkHashMap);
    }
    List <InventoryDesc> inventoryDescs = null;
    @RequestMapping("/searchDescIdd")
    @ResponseBody
    public String searchDescIdd(HttpServletRequest req){
        String proId=req.getParameter("proId");
        InventoryDesc inventoryDesc = new InventoryDesc();
        inventoryDesc.setProId(proId);
        inventoryDesc.setInveDescDepot(inveDepot);
        inventoryDescs = checkServiceInter.searchDescProId(inventoryDesc);

        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("data",inventoryDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping(value = {"/updateInvencheckDesc/{inveInfo}"},method = RequestMethod.GET)
    @ResponseBody
    public void updateInvencheckDesc(@PathVariable(name = "inveInfo") String inveInfo) throws Exception{
        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("[" + inveInfo + "]");
        //调用自定义的转换工具类
        List<InventoryDesc> inventoryDescList = InventoryUtil.decomposeJsonArray(stringBuffer, InventoryDesc.class);
        int i = checkServiceInter.updateInventoryCheckDesc(inventoryDescList);
    }
    int count;
    @RequestMapping("/sraechSta")
    @ResponseBody
    public String sraechSta(HttpServletRequest req){
        String invChId=req.getParameter("invChId");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(invChId);
        //得出盘点单的商品数量
        for(int i=0;i<inventoryCheckDescs.size()-1;i++){
            Integer a= inventoryCheckDescs.get(i).getInvCheckDescCheckNum();
        }
        count=inventoryCheckDescs.size();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryCheckDescs.size());
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/searchStatementyp")
    @ResponseBody
    public String searchStatementyp(HttpServletRequest req){
        String invChId=req.getParameter("invChId");
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchStatementyp(invChId);
        //得出盘点单的商品数量
        for(int i=0;i<inventoryCheckDescs.size()-1;i++){
            Integer a= inventoryCheckDescs.get(i).getInvCheckDescCheckNum();
        }
        count=inventoryCheckDescs.size();
        HashMap <Object, Object> checkHashMap = new HashMap <>();
        checkHashMap.put("code",0);
        checkHashMap.put("msg","");
        checkHashMap.put("count",inventoryCheckDescs.size());
        checkHashMap.put("data",inventoryCheckDescs);
        return JSON.toJSONString(checkHashMap);
    }
    @RequestMapping("/updateCheckDescIdupdateCheckDescId")
    @ResponseBody
    public void updateCheckDescId(HttpServletRequest req){
        String id=req.getParameter("id");
        int i = checkServiceInter.updateCheckDescId(id);
    }
    //查询仓库
    @RequestMapping("/searchDepotInfo")
    @ResponseBody
    public String searchDepotInfo() {
        List <DepotInfo> depotInfos = checkServiceInter.searchDepotInfowx();
        return JSON.toJSONString(depotInfos);
    }
    @RequestMapping("/updateInvenCheck")
    @ResponseBody
    public void updateInvenCheck(HttpServletRequest req) throws ParseException {
        String checkData=req.getParameter("checkData");
        String depId=req.getParameter("depId");
        String comment=req.getParameter("comment");
        String invChId = req.getParameter("invChId");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(checkData);
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchTakeStockId(invChId);
        int c=0;
        for(int i=0;i<inventoryCheckDescs.size();i++){
            Integer a= inventoryCheckDescs.get(i).getInvCheckDescCheckNum();
            c+=a;
        }
        if(count>0){
            InventoryCheck inventoryCheck = new InventoryCheck();
            inventoryCheck.setInvCheckId(invChId);
            inventoryCheck.setInvCheckDate(parse);
            inventoryCheck.setDepotId(inveDepot);
            inventoryCheck.setEmpId("admin");
            inventoryCheck.setInvCheckProBar(count);
            inventoryCheck.setInvCheckProNum(c);
            inventoryCheck.setInvCheckProComment(comment);
            int i = checkServiceInter.updateInvenCheck(inventoryCheck);
        }
    }

    @RequestMapping(value = {"/updateInvenDesc/{descInfo}"},method = RequestMethod.GET)
    @ResponseBody
    public void updateInvenDesc(@PathVariable(name = "descInfo") String descInfo) throws Exception{
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[" + descInfo + "]");
        List<InventoryCheckDesc> inventoryDescList = InventoryUtil.decomposeJsonArray(stringBuffer, InventoryCheckDesc.class);
        int i = checkServiceInter.updateInvenDesc(inventoryDescList);
    }
    @RequestMapping(value = "/updateBreakage",method = RequestMethod.GET)
    @ResponseBody
    public void updateBreakage() throws Exception{
        List <InventoryCheckDesc> inventoryCheckDescs = checkServiceInter.searchChane(arrty);
        for(InventoryCheckDesc i : inventoryCheckDescs){
            if(i.getQuantityVariance()<0){
                i.setQuantityVariance(Math.abs(i.getQuantityVariance()));
                int b = checkServiceInter.updateBreakage(i);
            }else{
                int i1 = checkServiceInter.updateOverflow(i);
            }
        }
//        if(q<0){
//            int b = checkServiceInter.updateBreakage(inventoryCheckDescs);
//        }else{
//            int i1 = checkServiceInter.updateOverflow(inventoryCheckDescs);
//        }
    }
    @RequestMapping("/updateInvenCheckId")
    @ResponseBody
    public void updateInvenCheckId(){
        int i = checkServiceInter.updateInvenCheckId(arrty);
    }
    @RequestMapping(value = {"/updateCheckNum/{inveInfo}"},method = RequestMethod.GET)
    @ResponseBody
    public void updateCheckNum(@PathVariable(name = "inveInfo") String inveInfo) throws Exception{
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[" + inveInfo + "]");
        //调用自定义的转换工具类
        List<InventoryDesc> inventoryDescList = InventoryUtil.decomposeJsonArray(stringBuffer, InventoryDesc.class);
    }
}
